<template>
	<view class="container" style="padding: 20rpx;">
		<!-- 加载状态 -->
		<view v-if="loading" class="d-flex justify-content-center align-items-center" style="height: 400rpx;">
			<uv-loading-icon mode="circle" color="#409eff" size="40"></uv-loading-icon>
			<text style="margin-left: 20rpx; color: #666;">订单加载中...</text>
		</view>
		
		<!-- 订单内容 -->
		<view v-else style="padding-bottom: 100rpx">
			<view class="bg-white">
				<view class="section d-flex flex-column" v-if="order.paid == 0">
					<view class=" d-flex flex-column" style=" margin-bottom: 20rpx; margin-top: 20rpx; ">
						<view style="font-size: 30rpx; margin-left: 20rpx; margin-bottom: 20rpx;">待支付</view>
						<view v-if="order.paid == 0" style=" margin: 0 20rpx 0;">
							<view class="mr-1"><uv-button type="success" v-if="order.status == 0" :plain="true"
									size="large" text="去支付" @click="pay()"></uv-button></view>
						</view>
					</view>
				</view>
				<view class="section">
					<!-- store info begin -->
					<list-cell :hover="false" v-if="['takeout','takein'].indexOf(order.orderType) > 0">
						<view class="w-100 d-flex align-items-center">
							<view class="d-flex flex-column w-60">
								<view class="w-100 font-size-lg text-color-base text-truncate">
									{{ (order.shop && order.shop.name !== undefined && order.shop.name !== null) ? order.shop.name : 'none' }}
								</view>
							</view>
							<view class="d-flex justify-content-end align-items-center w-40">
								<view class="iconfont-yshop icon-mobile" @click="makePhoneCall(order.shop)"
									style="font-size: 45rpx; margin-right: 40rpx">
								</view>
								<view class="iconfont-yshop icon-location" @click="openLocation(order.shop)"
									style="font-size: 45rpx"></view>
							</view>
						</view>
					</list-cell>
					<!-- store info end -->
					<list-cell :hover="false" padding="50rpx 30rpx">
						<view class="w-100 d-flex flex-column">
							<view class="d-flex align-items-center just-content-center">
								<view class="sort-num">{{ order.pickupCode || '暂无' }}</view>
							</view>
							<!-- steps begin -->
							<view class="d-flex just-content-center">
								<view class="steps d-flex flex-column w-80">
									<view class="steps__img-column">
										<view class="steps__img-column-item">
											<view class="iconfont-yshop icon-lamp" v-if="order.paid == 1"></view>
											<view class="iconfont-yshop icon-lamp unactive" v-else></view>
										</view>
										<view class="steps__img-column-item">
											<view class="iconfont-yshop icon-daojishi"
												v-if="order.paid == 1 && order.status > 0"></view>
											<view class="iconfont-yshop icon-daojishi unactive" v-else></view>
										</view>
										<view class="steps__img-column-item" v-if="order.orderType == 'takeout'">
											<view class="iconfont-yshop icon-takeout" v-if="order.status > 1"></view>
											<view class="iconfont-yshop icon-takeout unactive" v-else></view>
										</view>
										<view class="steps__img-column-item">
											<view class="iconfont-yshop icon-doorbell" v-if="order.status > 2"></view>
											<view class="iconfont-yshop icon-doorbell unactive" v-else></view>
										</view>
									</view>
									<view class="steps__text-column">
										<view class="steps__text-column-item active">
											<view class="steps__column-item-line bg-transparent"></view>
											<view class="steps__text-column-item-text">已下单</view>
											<view class="steps__column-item-line"></view>
										</view>
										<view class="steps__text-column-item activ"
											:class="{ active: order.paid == 1 }">
											<view class="steps__column-item-line"></view>
											<view class="steps__text-column-item-text">制作中</view>
											<view class="steps__column-item-line"></view>
										</view>
										<view class="steps__text-column-item" :class="{ active: order.status > 1 }"
											v-if="order.orderType == 'takeout'">
											<view class="steps__column-item-line"></view>
											<view class="steps__text-column-item-text">配送中</view>
											<view class="steps__column-item-line bg-transparent"></view>
										</view>
										<view class="steps__text-column-item" :class="{ active: order.status > 2 }">
											<view class="steps__column-item-line"></view>
											<view class="steps__text-column-item-text">
												{{ order.orderType == "takeout" ? "已送达" : "请取餐" }}
											</view>
											<view class="steps__column-item-line bg-transparent"></view>
										</view>
									</view>

								</view>

							</view>
							<!-- <view v-if="order.status == 0">
								<button>支付</button>
							</view> -->
							<!-- steps end -->
							<view v-if="order.status == 0 && order.paid > 0"
								class="d-flex just-content-center align-items-center font-size-base text-color-assist mb-40">
								您前面还有
								<text class="text-color-primary mr-10 ml-10">{{
                  order.preNum
                }}</text>
								单待制作
							</view>
							<!-- goods begin -->
							<view class="w-100 d-flex flex-column position-relative mt-30"
								style="margin-bottom: -40rpx">
								<view class="w-100 d-flex align-items-center mb-40"
									v-for="(good, index) in order.products" :key="index">
									<view class="d-flex flex-column w-60 overflow-hidden">
										<view class="font-size-lg text-color-base mb-10 text-truncate">{{ good.title }}
										</view>
										<view class="font-size-sm text-color-assist text-truncate">{{ good.spec }}
										</view>
									</view>
									<view class="d-flex w-40 align-items-center justify-content-between pl-30">
										<view class="font-size-base text-color-base">x{{ good.number }}</view>
										<view class="font-size-base text-color-base font-weight-bold">￥{{ good.price }}
										</view>
									</view>
								</view>
							</view>
							<!-- goods end -->
						</view>
					</list-cell>
				</view>
				<view class="section">
					<!-- goods begin -->
					<list-cell :hover="false" padding="30rpx 30rpx">
						<view class="w-100 d-flex flex-column position-relative">
							<view class="w-100 d-flex align-items-center mb-40" v-for="(good, index) in order.cartInfo"
								:key="index">
								<image :src="good.image" mode="aspectFill" class="image"></image>
								<view class="d-flex flex-column w-60 overflow-hidden">
									<view class="font-size-lg text-color-base mb-10 text-truncate">{{ good.title }}
									</view>
									<view class="font-size-sm text-color-assist text-truncate">{{
                    good.spec
                  }}</view>
								</view>
								<view class="d-flex w-40 align-items-center justify-content-between pl-30">
									<view class="font-size-base text-color-base">x{{ good.number }}</view>
									<view class="font-size-base text-color-base font-weight-bold">￥{{ good.price }}
									</view>
								</view>
							</view>
						</view>
					</list-cell>
					<!-- goods end -->
					<!-- payment and amount begin -->
					<list-cell :hover="false" padding="50rpx 30rpx">
						<view class="w-100 d-flex flex-column">
							<view class="pay-cell">
								<view>支付方式</view>
								<view class="font-weight-bold">{{
                  order.statusDto.payType
                }}</view>
							</view>
							<view class="pay-cell">
								<view>订单金额</view>
								<view class="font-weight-bold">￥{{ order.totalPrice }}</view>
							</view>
							<view class="pay-cell" v-if="order.orderType == 'takeout'">
								<view>配送费</view>
								<view class="font-weight-bold">￥{{ order.payPostage }}</view>
							</view>
							<view class="pay-cell">
								<view>优惠金额</view>
								<view class="font-weight-bold">￥{{ order.couponPrice }}</view>
							</view>
							<view class="pay-cell">
								<view>实付金额</view>
								<view class="font-weight-bold">￥{{ order.payPrice }}</view>
							</view>
						</view>
					</list-cell>
					<!-- payment and amount end -->
				</view>
				<view class="section">
					<!-- order info begin -->
					<list-cell :hover="false" padding="50rpx 30rpx">
						<view class="w-100 d-flex flex-column">
							<view class="pay-cell">
								<view>下单时间</view>
								<view class="font-weight-bold">{{ order.createTime }}</view>
							</view>
							<view class="pay-cell" v-if="order.shop">
								<view>下单门店</view>
								<view class="font-weight-bold">
									{{ (order.shop && order.shop.name !== undefined && order.shop.name !== null) ? order.shop.name : 'none' }}
								</view>
							</view>
							<view class="pay-cell">
								<view>订单号</view>
								<view class="font-weight-bold">{{ order.orderId }}</view>
							</view>
						</view>
					</list-cell>
					<!-- order info end -->
				</view>
				<!-- order other info begin -->
				<list-cell :hover="false" padding="50rpx 30rpx 20rpx" last
					v-if="order.orderType == 'take_out' || order.orderType == 'take_in'">
					<view class="w-100 d-flex flex-column">
						<view class="pay-cell">
							<view>享用方式</view>
							<view class="font-weight-bold">{{
                order.orderType == "takein" ? "自取" : "外卖"
              }}</view>
						</view>
						<view class="pay-cell">
							<view>取餐时间</view>
							<view class="font-weight-bold">{{
                order.getTime ? order.getTime : "立即取餐"
              }}</view>
						</view>
						<view class="pay-cell">
							<view>制作完成时间</view>
							<view class="font-weight-bold">{{
                order.deliveryTime ? order.deliveryTime : "无"
              }}</view>
						</view>
						<view class="pay-cell">
							<view>备注</view>
							<view class="font-weight-bold">{{
                order.remark ? order.remark : "无"
              }}</view>
						</view>
					</view>
				</list-cell>
				<!-- order other info end -->
			</view>
			<view class="fixed-bottom flex justify-end bg-white p-2" v-if="order.paid > 0 && order.refundStatus == 0">
				<view class="mr-1"><uv-button type="success" v-if="order.status < 2" :plain="true" size="small"
						text="确认收到餐" @click="receive(order)"></uv-button></view>
				<!-- <view><uv-button type="warning" :plain="true" size="small" text="申请退款" @click="refund(order)"></uv-button></view> -->
			</view>
		</view>
	</view>
</template>

<script setup>
	import debounce from '@/utils/debounce.js'

	import {
		ref
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		formatDateTime
	} from '@/utils/util'
	import {
	payUnify,
	orderDetail,
	orderReceive
} from "@/api/order";
import {
	ensurePlatformId
} from '@/utils/platformId';
import {
	alipayMiniAppLogin
} from '@/api/auth';

	const loading = ref(true);
	const order = ref({
		id: '',
		orderId: '',
		paid: 0,
		status: 0,
		orderType: '',
		pickupCode: '',
		totalPrice: 0,
		payPrice: 0,
		payPostage: 0,
		couponPrice: 0,
		createTime: '',
		getTime: '',
		deliveryTime: '',
		remark: '',
		preNum: 0,
		refundStatus: 0,
		payType: '',
		shop: {
			name: "",
			mobile: "",
			lat: "",
			lng: "",
			address: "",
			addressMap: ""
		},
		statusDto: {
			payType: "",
		},
		products: [],
		cartInfo: []
	});
	const numForMading = ref(5);

	onLoad((option) => {
		detail(option.id);
	});

	const getPaymentPrice = () => {
		if (order.value) {
			return order.value.payPrice;
		}
		return "";
	}
	const detail = async (id) => {
		try {
			loading.value = true;
			let data = await orderDetail(id);
			if (data) {
				// 合并数据，保持默认值
				order.value = {
					...order.value,
					...data,
					shop: {
						...order.value.shop,
						...(data.shop || {})
					},
					statusDto: {
						...order.value.statusDto,
						...(data.statusDto || {})
					},
					products: data.products || [],
					cartInfo: data.cartInfo || []
				};
				console.log('订单详情加载成功:', order.value);
			} else {
				console.error('订单详情数据为空');
				uni.showToast({
					title: '订单数据加载失败',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('加载订单详情失败:', error);
			uni.showToast({
				title: '网络错误，请重试',
				icon: 'none'
			});
		} finally {
			loading.value = false;
		}
	};
	const openLocation = () => {
		let shop = order.value.shop;
		uni.openLocation({
			address: shop.addressMap + shop.address + " " + (shop && shop.name !== undefined && shop.name !==
				null ? shop.name : ''),
			latitude: parseFloat(shop.lat),
			longitude: parseFloat(shop.lng),
			fail(res) {
				console.log(res);
			},
		});
	};
	const makePhoneCall = () => {
		let shop = order.value.shop;
		uni.makePhoneCall({
			phoneNumber: shop.mobile,
			fail(res) {
				console.log(res);
			},
		});
	};

	// 确认收到货
	const receive = async (order) => {
		let data = await orderReceive({
			uni: order.orderId,
		});
		if (data && data.code == 0) {
			await detail(order.id);
		}
	};
	//提交退款
	const refund = (order) => {
		uni.navigateTo({
			url: "/pages/components/pages/orders/refund?orderId=" +
				order.orderId +
				"&payPrice=" +
				order.payPrice +
				"&totalPrice=" +
				order.totalPrice,
		});
	};
	const pay = async () => {
		console.log('will pay');
		console.log(order.value);
		console.log(order.value.payType);
		if (!order.value) {
			console.log('订单不存在');
			return;
		}

		// 支付前确保平台ID存在
		if (order.value.payType === 'weixin' || order.value.payType === 'alipay') {
			const hasPlatformId = await ensurePlatformId('payment');
			if (!hasPlatformId) {
				uni.showToast({
					title: '需要授权后才能支付',
					icon: 'none'
				});
				return;
			}
		}

		if (order.value.payType == 'weixin') {
			// 微信支付
			weixinPay(order.value);
		} else if (order.value.payType == 'yue') {
			// 余额支付
			balancePay(order.value);
		} else if (order.value.payType == 'alipay') {
			// alipay
			aliPay(order.value);
		} else {
			// 其他支付方式
			uni.showToast({
				title: '不支持的支付方式',
				icon: 'none'
			});
			return;
		}
	}
	const balancePay = async () => {

	}
	const getAlipayBuyerId = async () => {
		return new Promise((resolve, reject) => {
			// 获取支付宝用户授权码
			my.getAuthCode({
				scopes: ['auth_user'],
				success: async (res) => {
					console.log('Auth code:', res.authCode);
					try {
						// 使用 auth.js 中的方法
						const result = await alipayMiniAppLogin({
							code: res.authCode
						});
						
						console.log('登录成功:', result);
						if (result && result.buyerId) {
							// 存储 buyer_id
							uni.setStorageSync('buyer_id', result.buyerId);
							resolve(result.buyerId);
						} else {
							console.error('未获取到 buyerId');
							reject(new Error('未获取到 buyerId'));
						}
					} catch (error) {
						console.error('登录失败:', error);
						reject(error);
					}
				},
				fail: (err) => {
					console.error('获取授权码失败:', err);
					reject(err);
				}
			});
		});
	};
	const aliPay = async (order) => {
		try {
			// 确保有 buyerId
			await getAlipayBuyerId();
			
			let from = 'routine';
			// #ifdef MP-ALIPAY
			from = 'alipay';
			// #endif

			let data = await payUnify({
				uni: order.orderId,
				from: from,
				paytype: 'alipay',
				buyer_id: uni.getStorageSync('buyer_id') // 使用存储的 buyer_id
			});

			console.log('alipay result:', data);

			if (!data) {
				uni.hideLoading();
				return;
			}

			if (data.trade_type === 'JSAPI' || data.trade_type === 'JSAPI_PAY') {
				// 支付宝小程序支付
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: data.data, // 支付宝订单信息
					success: function(res) {
						console.log('支付成功:', res);
						// 刷新当前页面的订单信息
						detail(order.id);
						uni.showToast({
							title: '支付成功',
							icon: 'success'
						});
					},
					fail: function(err) {
						console.error('支付失败:', err);
						
						// 详细的错误处理
						let errorMsg = '支付失败';
						if (err.memo && err.memo.includes('API 代理')) {
							errorMsg = '开发环境配置问题，请在支付宝开发者工具中开启API代理';
						} else if (err.resultCode === '4000') {
							errorMsg = '支付环境配置错误';
						} else if (err.resultCode === '6001') {
							errorMsg = '用户取消支付';
						} else if (err.resultCode === '6002') {
							errorMsg = '网络连接出错';
						}
						
						uni.showToast({
							title: errorMsg,
							icon: 'none',
							duration: 3000
						});
					}
				});
			} else {
				console.error('Unsupported trade type for Alipay:', data.trade_type);
				uni.showToast({
					title: '不支持的支付类型',
					icon: 'none'
				});
			}
		} catch (error) {
			console.error('支付宝支付异常:', error);
			uni.hideLoading();
			uni.showToast({
				title: '获取支付授权失败，请重试',
				icon: 'none'
			});
		}
	}
	const weixinPay = async (order) => {
		let from = 'routine'
		// #ifdef H5
		from = 'h5'
		if (isWeixin()) {
			from = 'wechat'
		}

		// #endif
		//let that = this;
		let data = await payUnify({
			uni: order.orderId,
			from: from,
			paytype: 'weixin'
		});
		console.log('wxpay result:', data)
		if (!data) {
			uni.hideLoading();
			return;
		}
		if (data.trade_type == 'JSAPI') {
			console.log('param:', data)


			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: data.data.timeStamp,
				nonceStr: data.data.nonceStr,
				package: data.data.package,
				signType: 'MD5',
				paySign: data.data.paySign,
				success: function(res) {
					// uni.removeStorageSync('cart');
					uni.setStorageSync('cart', []);
					uni.switchTab({
						url: '/pages/order/order'
					});
				},
				fail: function(err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});

		}
	}
</script>

<style lang="scss" scoped>
	.image {
		width: 120rpx;
		height: 120rpx;
		margin-right: 30rpx;
		border-radius: 8rpx;
	}

	.pay-cell {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: $font-size-base;
		color: $text-color-base;
		margin-bottom: 40rpx;

		&:nth-last-child(1) {
			margin-bottom: 0;
		}
	}

	.invote-box {
		position: absolute;
		width: 100%;
		left: 0;
		top: 0;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 30rpx;
			height: 30rpx;
		}
	}

	.btn-box {
		background-color: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 120rpx;
		box-shadow: 0 0 20rpx rgba($color: #000000, $alpha: 0.1);
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		z-index: 11;

		.item {
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 10rpx;
			flex: 1;
			flex-shrink: 0;

			button {
				width: 100%;
				border-radius: 50rem !important;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0;
			}
		}
	}

	/* #ifdef H5 */
	page {
		min-height: 100%;
		background-color: $bg-color;
	}

	/* #endif */
	.order-box {
		padding: 20rpx;
		/* #ifdef H5 */
		margin-bottom: 100rpx;
		/* #endif */
	}

	.drinks-img {
		width: 260rpx;
		height: 260rpx;
	}

	.tips {
		margin: 60rpx 0 80rpx;
		line-height: 48rpx;
	}

	.drink-btn {
		width: 320rpx;
		border-radius: 50rem !important;
		margin-bottom: 40rpx;
		font-size: $font-size-base;
		line-height: 3;
	}

	@mixin arch {
		content: "";
		position: absolute;
		background-color: $bg-color;
		width: 30rpx;
		height: 30rpx;
		bottom: -15rpx;
		z-index: 10;
		border-radius: 100%;
	}

	.section {
		position: relative;

		&::before {
			@include arch;
			left: -15rpx;
		}

		&::after {
			@include arch;
			right: -15rpx;
		}
	}

	.pay-cell {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: $font-size-base;
		color: $text-color-base;
		margin-bottom: 40rpx;

		&:nth-last-child(1) {
			margin-bottom: 0;
		}
	}

	.sort-num {
		font-size: 64rpx;
		font-weight: bold;
		color: $text-color-base;
		line-height: 2;
	}

	.steps__img-column {
		display: flex;
		margin: 30rpx 0;

		.steps__img-column-item {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 80rpx;
				height: 80rpx;
			}

			.unactive {
				color: #919293;
			}
		}
	}

	.steps__text-column {
		display: flex;
		margin-bottom: 40rpx;

		.steps__text-column-item {
			flex: 1;
			display: inline-flex;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: $font-size-base;
			color: $text-color-assist;

			&.active {
				color: $text-color-base;
				font-weight: bold;

				.steps__column-item-line {
					background-color: $text-color-base;
				}
			}

			.steps__column-item-line {
				flex: 1;
				height: 2rpx;
				background-color: #919293;
				transform: scaleY(0.5);
			}

			.steps__text-column-item-text {
				margin: 0 8px;
			}
		}
	}

	.icon-lamp,
	.icon-daojishi,
	.icon-takeout,
	.icon-doorbell {
		font-size: 60rpx;
	}

	.iconfont-yshop {
		color: #dd524d;
	}
</style>